package com.aplink.generic.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.bitmap.ImageCache;
import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.ImageViewSpec;
import com.aplink.generic.util.ResourcesHelper;

public class ApLinkImageView extends ApLinkBaseView {
    private static final String IMAGE_CACHE_DIR = "images";
    /**
     * 
     */
    private static final long serialVersionUID = -1428746969468186824L;
    private ImageFetcher mImageFetcher;
    private final ImageView mImageView;
    private final ImageViewSpec mImageViewSpec;

    public ApLinkImageView(final GenericFragment genericFragment,
            final ControlSpec controlSpec, final View view) {
        super(genericFragment, controlSpec, view);
        this.mImageView = (ImageView) view;
        this.mImageViewSpec = (ImageViewSpec) controlSpec;
        final Context context = GenericApplication.getInstance()
                .getApplicationContext();
        if ((this.mImageViewSpec.getImageWidth() + this.mImageViewSpec
                .getImageHeight()) == 0) {
            final DisplayMetrics displayMetrics = context.getResources()
                    .getDisplayMetrics();
            final int height = displayMetrics.heightPixels;
            final int width = displayMetrics.widthPixels;
            final int longest = (height > width ? height : width) / 2;
            this.mImageFetcher = new ImageFetcher(context, longest);
        } else {
            this.mImageFetcher = new ImageFetcher(context,
                    this.mImageViewSpec.getImageWidth(),
                    this.mImageViewSpec.getImageHeight());
        }
        final ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
                context, ApLinkImageView.IMAGE_CACHE_DIR);
        cacheParams.setMemCacheSizePercent(0.25f);
        this.mImageFetcher.addImageCache(getChildFragmentManager(), cacheParams);
        this.mImageFetcher.setImageFadeIn(true);
    }

    @Override
    public void clear() {
        this.mImageView.setImageDrawable(null);
    }

    @Override
    public GenericAttribute getAttribute() {
        return null;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public ImageView getView() {
        return this.mImageView;
    }

    @Override
    protected void initViewProperties() {
        final boolean isVisibility = this.mImageViewSpec.isVisibility();
        if (!isVisibility) {
            this.mImageView.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void loadBindingObject() {
        String imageUrl = getPassedContent();
        if (imageUrl == null) {
            final String dataObj = this.mImageViewSpec.getDataObject();
            final String mappingName = this.mImageViewSpec.getMappingName();
            imageUrl = getPassedGenericObject().getValue(
                    String.format("%s.%s", dataObj, mappingName)).toString();
        }
        if (imageUrl != null) {
            this.mImageFetcher.setReadOnly(this.mImageViewSpec.isReadOnly());
            this.mImageFetcher.setMaxZoom(this.mImageViewSpec.getMaxZoom());
            this.mImageFetcher.setMinZoom(this.mImageViewSpec.getMinZoom());
            if (!TextUtils.isEmpty(this.mImageViewSpec.getDefaultImage())) {
                final int resId = ResourcesHelper
                        .getDrawableIdByName(this.mImageViewSpec
                                .getDefaultImage());
                this.mImageFetcher.setLoadingImage(resId);
            }
            this.mImageFetcher.loadImage(imageUrl, this.mImageView);
        }
    }

    @Override
    protected void loadDefaultObject() {
    }

    @Override
    protected void setEventListener() {

    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
